/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.flow.drawingprocess.service.impl;

import io.reactivex.internal.operators.flowable.FlowableUnsubscribeOn;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.helper.DataUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.flow.business.service.IFlowService;
import org.springblade.flow.core.constant.ProcessConstant;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.flow.core.utils.ExpenseUtil;
import org.springblade.flow.core.utils.FlowUtil;
import org.springblade.flow.core.utils.TaskUtil;
import org.springblade.flow.drawingprocess.entity.DrawingProcess;
import org.springblade.flow.drawingprocess.vo.DrawingProcessVO;
import org.springblade.flow.drawingprocess.mapper.DrawingProcessMapper;
import org.springblade.flow.drawingprocess.service.IDrawingProcessService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.unit.DataUnit;

/**
 *  服务实现类
 *
 * @author LY
 * @since 2021-01-25
 */
@Service
@Slf4j
@AllArgsConstructor
public class DrawingProcessServiceImpl extends BaseServiceImpl<DrawingProcessMapper, DrawingProcess> implements IDrawingProcessService {

	private final IFlowService flowService;
	@Override
	public IPage<DrawingProcessVO> selectDrawingProcessPage(IPage<DrawingProcessVO> page, DrawingProcessVO drawingProcess) {
		return page.setRecords(baseMapper.selectDrawingProcessPage(page, drawingProcess));
	}

	/**
	 * 开始流程节点得审批，需要和事务联系在一起
	 * @param drawingProcess
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean startProcess(DrawingProcess drawingProcess) {
		String businessTable = ExpenseUtil.getDrawingProcessTable(ProcessConstant.EXPENSE_KEY);
		// 设置节点得审批信息(通过角色区分)


		if (Func.isEmpty(drawingProcess.getId())) {
			// 保存
			drawingProcess.setApplyTime(DateUtil.now());
			save(drawingProcess);
			// 启动流程
			Kv variables =  Kv.create()
				// 设置创建人信息
			    .set(ProcessConstant.TASK_VARIABLE_CREATE_USER, AuthUtil.getUserName())
            	// 设置审批节点
	            .set("leader", TaskUtil.getTaskUser("1123598821738675201"));

			BladeFlow flow = flowService.startProcessInstanceById(drawingProcess.getProcessDefinitionId(),
				ExpenseUtil.getDrawingProcessKey(businessTable, String.valueOf(drawingProcess.getId())), variables);
			if (Func.isNotEmpty(flow)) {
				log.debug("流程已启动,流程ID:" + flow.getProcessInstanceId());
				// 返回流程id写入leave
				drawingProcess.setProcessInstanceId(flow.getProcessInstanceId());
				updateById(drawingProcess);
			} else {
				throw new ServiceException("开启流程失败");
			}
		}else{
			updateById(drawingProcess);
		}
		return true;
	}

}
